package com.itany.corejava.day17;

import com.itany.corejava.day08.Tank;
import com.itany.corejava.day13.Calculator;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.stream.Collectors;

//通过Class对象获得类里的信息
public class TestClass2 {

    public static void main(String[] args) {
        Class c=Person.class;



        //包
        Package pack=c.getPackage();
        System.out.println("package "+pack.getName()+";");




        //类名
        String cName=c.getName();
        cName=c.getSimpleName();
        System.out.print("public class "+cName+" ");




        //父类
        Class sClass=c.getSuperclass();
        System.out.print("extends "+sClass.getSimpleName()+" ");




        //父接口
        Class[] interfaces=c.getInterfaces();
        System.out.print("implements ");
//        for (Class interfaceClass : interfaces) {
//            System.out.print(interfaceClass.getSimpleName()+" ");
//        }
        String interfacesStr=Arrays.stream(interfaces)
                                   .map(Class::getSimpleName)
                                   .collect(Collectors.joining(","));
        System.out.print(interfacesStr+" ");
        System.out.println("{");



        //属性
        Field[] fields=c.getFields();
        fields=c.getDeclaredFields();
//        System.out.println(fields.length);
        for (Field field : fields) {
            //修饰符
            int mod=field.getModifiers();
//            System.out.println(mod);
            String modifier= Modifier.toString(mod);
            System.out.print("    "+modifier+" ");
            //类型
            System.out.print(field.getType().getSimpleName()+" ");
            //名字
            System.out.println(field.getName()+";");
        }


        System.out.println();


        //构造方法
        Constructor[] constructors=c.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            //修饰符
            int mod=constructor.getModifiers();
            System.out.print("    "+Modifier.toString(mod)+" ");
            //类名
            System.out.print(c.getSimpleName()+"(");
//            //参数列表类型
//            Class[] paramClasses=constructor.getParameterTypes();
//            String paramClassesStr=Arrays.stream(paramClasses)
//                                         .map(Class::getSimpleName)
//                                         .collect(Collectors.joining(","));
//            System.out.print(paramClassesStr+" ");
            //参数
            Parameter[] parameters=constructor.getParameters();
            for (Parameter parameter : parameters) {
                //参数类型
                System.out.print(parameter.getType().getSimpleName()+" ");
                //参数名
                System.out.print(parameter.getName()+" ");
            }
            System.out.print(") throws ");
            //抛出异常
            Class[] exceptionClasses=constructor.getExceptionTypes();
            String exceptionClassesStr=Arrays.stream(exceptionClasses)
                                             .map(Class::getSimpleName)
                                             .collect(Collectors.joining(","));
            System.out.println(exceptionClassesStr+" {}");
        }

        System.out.println();




        //方法
        Method[] methods=c.getDeclaredMethods();
        for (Method method : methods) {
            //修饰符
            int mod=method.getModifiers();
            System.out.print("    "+Modifier.toString(mod)+" ");
            //返回类型
            System.out.print(method.getReturnType().getSimpleName()+" ");
            //方法名
            System.out.print(method.getName()+"(");
//            //参数列表类型
//            Class[] paramClasses=method.getParameterTypes();
//            String paramClassesStr=Arrays.stream(paramClasses)
//                                         .map(Class::getSimpleName)
//                                         .collect(Collectors.joining(","));
//            System.out.print(paramClassesStr+" ");
            //参数
            Parameter[] parameters=method.getParameters();
            for (Parameter parameter : parameters) {
                //参数类型
                System.out.print(parameter.getType().getSimpleName()+" ");
                //参数名
                System.out.print(parameter.getName()+" ");
            }
            System.out.print(") throws ");
            //抛出异常
            Class[] exceptionClasses=method.getExceptionTypes();
            String exceptionClassesStr=Arrays.stream(exceptionClasses)
                                             .map(Class::getSimpleName)
                                             .collect(Collectors.joining(","));
            System.out.println(exceptionClassesStr+" {}");
        }



        System.out.println("}");








    }





}
